/*************************************************************************
 * Nuvoton Technology Corporation confidential
 *
 * Copyright (c) 2009 by Nuvoton Technology Corporation
 * All rights reserved
 * 
 * FILENAME
 *     atapi.c
 *
 * VERSION
 *     1.0
 *
 * DESCRIPTION
 *     This file contains ATAPI library APIs.
 *
 * HISTORY
 *     2008.06.25		Created
 *
 * REMARK
 *     None
 **************************************************************************/
/* Header files */
#include <stdio.h>
#include <string.h>

#include "Atapi.h"
#include "ide.h"

UCHAR gScatterGather = 0; 		//default, no Scatter Gather DMA function
INT recATAPI_EngineCLK;
//------------------------------------------------------------------------
//   The init function of ATAPI device library. Before use this function,
//	the ATAPI engine clock and scatter gather function should be defined.
//
//  In : ATAPI_EngineCLK : Clock for ATAPI engine
//
//  Ret: 0 : fail
//       1 : success
//------------------------------------------------------------------------
INT atapiInit(INT ATAPI_EngineCLK)
{	
	recATAPI_EngineCLK = ATAPI_EngineCLK;
	
	return (atapiInitial(ATAPI_EngineCLK));
}


//------------------------------------------------------------------------
//	Open ATAPI device and set transfer mode
//
//  Ret: 0 : fail
//       1 : success
//------------------------------------------------------------------------ 
INT atapiOpen(VOID)
{
	INT ret;
	
	ret = atapiInitTransferMode(recATAPI_EngineCLK);
	if(!ret)
		return 0;	
	
	return 1;
}

//------------------------------------------------------------------------
//	IO control functions for ATAPI device
//
//  In : atapiMasterSlave : which device to be controlled
//		 atapiCommand : Commands
//
//	Out :  atapiArg : Argument for the control
//
//  Ret: 0 : fail
//       1 : success
//------------------------------------------------------------------------ 
INT atapiIoctl(INT32 atapiMasterSlave, UINT32 atapiCommand, UINT32 atapiArg)
{
	UINT32 cmd;
	
	if(atapiMasterSlave > ATAPI_SLAVE || atapiMasterSlave < ATAPI_MASTER)
		return 0;
		
	switch(cmd)
	{
		case ATAPI_GET_CAPACITY:	//get the capacity of device
			*((UINT32 *)atapiArg) = atapiGetDriveCapacity(atapiMasterSlave);
			break;
		
		case ATAPI_SET_SCATTERGATHER_DMA:
			gScatterGather = atapiArg;    // set flag to decide if use Scatter Gather DMA
			break;
		
		case ATAPI_RESET_DEVICE:	//reset all the device
			atapiHWReset(atapiMasterSlave);
			break;
		
		case ATAPI_GET_UDMAMODE_INFO:
			*((UINT32 *)atapiArg) = IDE_HEADER_INFO[atapiMasterSlave].udma_modes;
			break;
			
		default:		//something wrong
			return 0;	
	}
	
	return 1;
}

//------------------------------------------------------------------------
//  Read method for ATAPI device
//
//  In : atapiDriveNo : Master or Slave device is selected
//	     DMAStartAddr : DMA starting address (non-cacheable)
//	     startLBA : starting LBA (bit 0-31) (sector)
//       count : number of sectors
//
//  Ret: 0 : fail
//       1 : success
//------------------------------------------------------------------------
INT atapiRead(UCHAR atapiDriveNo, UINT32 DMAStartAddr, UINT32 startLBA,  USHORT count)
{
	
	return atapiReadDMA(atapiDriveNo, DMAStartAddr, startLBA,  count);
}

//------------------------------------------------------------------------
//  Write method for ATAPI device
//
//  In : atapiDriveNo : Master or Slave device is selected
//	     DMAStartAddr : DMA starting address (non-cacheable)
//	     startLBA : starting LBA (bit 0-31) (sector)
//       count : number of sectors//  Ret: 0 : fail
//       1 : success
//------------------------------------------------------------------------
INT atapiWrite(UCHAR atapiDriveNo, UINT32 DMAStartAddr, UINT32 startLBA,  USHORT count)
{
	return atapiWriteDMA(atapiDriveNo, DMAStartAddr, startLBA,  count);
}
